<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("comctrls.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class Inv_details extends Page
{
       public $cmbMax = null;
       public $edMax = null;
       public $Label9 = null;
       public $lblre = null;
       public $lbllow = null;
       public $lblor = null;
       public $lblqty = null;
       public $Label12 = null;
       public $JSValidate = null;
       public $hfError = null;
       public $lbltype = null;
       public $lblname = null;
       public $Label38 = null;
       public $lblyear = null;
       public $Label48 = null;
       public $lblStocknum = null;
       public $JTDate = null;
       public $edLoc = null;
       public $btnAddLoc = null;
       public $btnDelLoc = null;
       public $listLoc = null;
       public $JTDivLoc = null;
       public $JTGroupBox1 = null;
       public $btnShowLoc = null;
       public $JTJavaScript3 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript1 = null;
       public $compid = null;
       public $imgBack = null;
       public $hiddenStockID = null;
       public $Image1 = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $cmbAlternateVend = null;
       public $cmbPrimaryVend = null;
       public $cmbLoc = null;
       public $edReorder = null;
       public $edLowStock = null;
       public $edQtyOrder = null;
       public $edQtyAvail = null;
       public $Label8 = null;
       public $Label7 = null;
       public $Label5 = null;
       public $Label6 = null;
       public $Label4 = null;
       public $Label3 = null;
       public $Label1 = null;
       public $Label2 = null;
       public $JTSiteTheme1 = null;
       function edReorderJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edReorder','lblre');
       <?php

       }

       function edLowStockJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edLowStock','lbllow');
       <?php

       }

       function edQtyOrderJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edQtyOrder','lblor');
       <?php

       }

       function edQtyAvailJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edQtyAvail','lblqty');
       <?php

       }


       function btnDelLocJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var locIndex = document.getElementById('listLoc').selectedIndex;
       var locValue = document.getElementById('listLoc')[locIndex].value;
       var compider3 = document.getElementById('compid').value;
       AjaxLoc3(locValue,compider3);
       <?php

       }

       function btnAddLocJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addloc = document.getElementById('edLoc').value;
       var compider = document.getElementById('compid').value;
       AjaxLoc2(addloc,compider);
       document.Inv_details.edLoc.value = "";
       <?php

       }

       function btnShowLocJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivLoc').Show();
       document.Inv_details.edLoc.value = "";
       <?php

       }





       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function btnCancelClick($sender, $params)
       {
       redirect('inventory_list.php?stock_id='.$this->hiddenStockID->getValue());


       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }

       function Inv_detailsCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblqty->Caption = '';
       $this->lbllow->Caption = '';
       $this->lblre->Caption = '';
       $this->lblor->Caption = '';

       $this->compid->Value = $_SESSION['company_id'];
       if($_SESSION['isAdmin']!=1)
       {
       $this->btnSubmit->setVisible(false);
       $this->btnCancel->setVisible(false);
       }

       if($_SESSION['access_part']==1)
       {
       $this->btnSubmit->setVisible(false);
       $this->btnCancel->setVisible(false);
       }

       if($_SESSION['access_part']==2)
       {
       $this->btnSubmit->setVisible(true);
       $this->btnCancel->setVisible(true);
       }


       $stock_id = $_GET['stock_id'];
       $this->hiddenStockID->setValue($stock_id);


       $result_invstocks = mysql_query("select * from invstocks_tbl where stock_id = '".$this->hiddenStockID->getValue()."'");
       $row_invstocks = mysql_fetch_array($result_invstocks);

       $this->lblStocknum->Caption = $row_invstocks['stock_num'];
       $this->lblname->Caption = $row_invstocks['description'];
       $this->lbltype->Caption = $row_invstocks['type'];

       $this->edQtyAvail->Text = $row_invstocks['qty_available'];


       $result_stock = mysql_query("select * from invstocks_status_tbl where stock_id='".$this->hiddenStockID->getValue()."'");
       $row_status = mysql_fetch_array($result_stock);

       $this->edQtyOrder->Text = $row_status['qty_on_order'];
       $this->edLowStock->Text = $row_status['low_stock_pt'];
       $this->edReorder->Text = $row_status['reorder_qty'];
       $this->JTDate->Text = $row_status['last_rcv_date'];
       $this->edMax->Text = $row_status['span_num'];
       $this->cmbMax->ItemIndex = $row_status['span_unit'];

       $this->cmbLoc->Clear();
       $this->cmbLoc->AddItem('--Select--', null, null);
       $this->cmbLoc->ItemIndex=$row_status['location'];
       $result_type = mysql_query("select * from lookup_inventory_tbl where company_id='".$_SESSION['company_id']."' AND category='storeroom/locations'");

        while($row_type = mysql_fetch_array($result_type))
        {
            $this->cmbLoc->AddItem($row_type['value'], null, $row_type['value']);
        }

        //list location
        $this->listLoc->Clear();
        $result_loc = mysql_query("select * from lookup_inventory_tbl where company_id='".$_SESSION['company_id']."' AND category='storeroom/locations'");

        while($row_loc = mysql_fetch_array($result_loc))
        {
            $this->listLoc->AddItem($row_loc['value'], null, $row_loc['value']);
        }

        //$this->cmbPrimaryVend->Clear();
        $this->cmbPrimaryVend->ItemIndex=$row_invstocks['vendor'];
        $result_vend = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

        while($row_vend = mysql_fetch_array($result_vend))
        {
            $this->cmbPrimaryVend->AddItem($row_vend['vendor_name'], null, $row_vend['vendor_name']);
        }


        $this->cmbAlternateVend->Clear();
        $this->cmbAlternateVend->AddItem('--Select--', null, null);
        $this->cmbAlternateVend->ItemIndex=$row_status['alt_vendor'];
        $result_alt = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

        while($row_alt = mysql_fetch_array($result_alt))
        {
            $this->cmbAlternateVend->AddItem($row_alt['vendor_name'], null, $row_alt['vendor_name']);
        }



       $this->imgBack->setLink('inventory_list.php?stock_id='.$this->hiddenStockID->getValue());

       }

       function btnSubmitClick($sender, $params)
       {
       $this->validateRequired($this->edQtyOrder->Text, 'lblor');
       $this->validateRequired($this->edReorder->Text, 'lblre');
       $this->validateRequired($this->edLowStock->Text, 'lbllow');
       $this->validateRequired($this->edQtyAvail->Text, 'lblqty');
       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {

      $result_status = mysql_query("select * from invstocks_status_tbl where stock_id='".$this->hiddenStockID->getValue()."'");
      $status_count = mysql_num_rows($result_status);
      if($status_count == 0)
      {
       mysql_query("INSERT INTO invstocks_status_tbl(company_id,
       stock_id,
       qty_available,
       qty_on_order,
       low_stock_pt,
       reorder_qty,
       location,
       alt_vendor,
       last_rcv_date,
       span_num,
       span_unit)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->hiddenStockID->getValue()."',
       '".$this->edQtyAvail->Text."',
       '".$this->edQtyOrder->Text."',
       '".$this->edLowStock->Text."',
       '".$this->edReorder->Text."',
       '".$this->cmbLoc->getItemIndex()."',
       '".$this->cmbAlternateVend->getItemIndex()."',
       '".$this->JTDate->Text."',
       '".$this->edMax->Text."',
       '".$this->cmbMax->getItemIndex()."')") or die("INSERT STATUS - ".mysql_error());

       $query_update_stock1 = "UPDATE invstocks_tbl SET qty_available = '".$this->edQtyAvail->Text."'
       where stock_id='".$this->hiddenStockID->getValue()."'";
       mysql_query($query_update_stock1) or die(mysql_error());
      }
      else
      {
      $query_update = "UPDATE invstocks_status_tbl SET
       qty_available ='".$this->edQtyAvail->Text."',
       qty_on_order ='".$this->edQtyOrder->Text."',
       low_stock_pt ='".$this->edLowStock->Text."',
       reorder_qty ='".$this->edReorder->Text."',
       location ='".$this->cmbLoc->getItemIndex()."',
       alt_vendor ='".$this->cmbAlternateVend->getItemIndex()."',
       last_rcv_date ='".$this->JTDate->Text."',
       span_num = '".$this->edMax->Text."',
       span_unit = '".$this->cmbMax->getItemIndex()."'
       where stock_id='".$this->hiddenStockID->getValue()."'";
       mysql_query($query_update) or die(mysql_error());

       $query_update_stock2 = "UPDATE invstocks_tbl SET qty_available = '".$this->edQtyAvail->Text."'
       where stock_id='".$this->hiddenStockID->getValue()."'";
       mysql_query($query_update_stock2) or die(mysql_error());

      }

      redirect('inventory_list.php?stock_id='.$this->hiddenStockID->getValue());
       }
       }

}

global $application;

global $Inv_details;

//Creates the form
$Inv_details=new Inv_details($application);

//Read from resource file
$Inv_details->loadResource(__FILE__);

//Shows the form
$Inv_details->show();

?>